home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / croutes.zip / JTOG.C < prev    next >
Text File  |  1980-01-01  |  3KB  |  89 lines

  1. /*                    *** jtog.c ***                                 */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* function to convert a julian date (1 = 1st day AD) into a         */
  6. /* gregorian date in the format mm/dd/yy.  Returns a 0 if successful */
  7. /* or a -1 if not.                                                   */
  8. /*                                                                   */
  9. /* WARNING - if outdate is not dimensioned at least 9 characters a   */
  10. /*           memory overwrite will occure.                           */
  11. /*                                                                   */
  12. /* Written by L. Cuthbertson, March 1984                             */
  13. /*                                                                   */
  14. /*********************************************************************/
  15. /*                                                                   */
  16.  
  17. #define CENTRY 19
  18. #define TRUE 1
  19. #define FALSE 0
  20.  
  21. int jtog(julian,outdate)
  22. char outdate[];
  23. register long julian;
  24. {
  25.     static int monthd[] = {0,31,59,90,120,151,181,212,243,273,304,334};
  26.     int centry,leap,i,iyr,imo,iday;
  27.     static long cdays = 36524, ydays = 365;
  28.  
  29.     /* reduce julian from 1st day AD to 1st day of CENTRY */
  30.     julian -= CENTRY*cdays + CENTRY/4;
  31.  
  32.     /* determine year */
  33.     iyr = julian/ydays;
  34.     if ((julian - (iyr*ydays + iyr/4)) < 0)
  35.         iyr -= 1;
  36.     if (iyr < 0)
  37.         return(-1);
  38.  
  39.     /* determine if this is a leap year or not */
  40.     if (iyr%4 == 0 && iyr != 0 || CENTRY%4 == 0)
  41.         leap = TRUE;
  42.     else
  43.         leap = FALSE;
  44.  
  45.     /* determine month */
  46.     julian = julian - (iyr*ydays + iyr/4);
  47.     if (leap) julian += 1;
  48.     for (imo=12;imo>0;imo--) {
  49.         i=0;
  50.         if ((leap) && (imo > 2)) i = -1;
  51.         if ((julian - monthd[imo-1] + i) > 0) break;
  52.     }
  53.     if (imo == 0) imo = 1;
  54.  
  55.     /* determine day */
  56.     iday = julian - monthd[imo-1] + i;
  57.     if (iday == 0) {
  58.         iyr -= 1;
  59.         if (iyr < 0) return(-1);
  60.         imo = 12;
  61.         iday = 31;
  62.     }
  63.  
  64.     /* transfer into output string */
  65.     if (imo > 9)
  66.         outdate[0] = '1';
  67.     else
  68.         outdate[0] = '0';
  69.     outdate[1] = (char)(imo%10 + '0');
  70.     outdate[2] = '/';
  71.  
  72.     if (iday > 9)
  73.         outdate[3] = (char)(iday/10 + '0');
  74.     else
  75.         outdate[3] = '0';
  76.     outdate[4] = (char)(iday%10 + '0');
  77.     outdate[5] = '/';
  78.  
  79.     if (iyr > 9)
  80.         outdate[6] = (char)(iyr/10 + '0');
  81.     else
  82.         outdate[6] = '0';
  83.     outdate[7] = (char)(iyr%10 + '0');
  84.     outdate[8] = '\0';
  85.  
  86.     /* done */
  87.     return(0);
  88. }
  89.